﻿@page "/system/tenants"
@using CleanArchitecture.Blazor.Application.Features.Tenants.DTOs
@using CleanArchitecture.Blazor.Application.Features.Tenants.Mappers
@using CleanArchitecture.Blazor.Application.Features.Tenants.Queries.Pagination
@using CleanArchitecture.Blazor.Application.Features.Tenants.Caching
@using CleanArchitecture.Blazor.Application.Features.Tenants.Commands.AddEdit
@using CleanArchitecture.Blazor.Application.Features.Tenants.Commands.Delete

@attribute [Authorize(Policy = Permissions.Tenants.View)]
@inject IStringLocalizer<Tenants> L

<PageTitle>@Title</PageTitle>

<MudDataGrid ServerData="@(ServerReload)"
             FixedHeader="true"
             FixedFooter="false"
             Height="calc(100vh - 300px)"
             Loading="@_loading"
             @bind-RowsPerPage="_defaultPageSize"
             MultiSelection="true"
             @bind-SelectedItems="_selectedItems"
             @bind-SelectedItem="_currentDto"
             Hover="true" @ref="_table">
    <ToolBarContent>
         <MudStack Row Spacing="0" Class="flex-grow-1" Justify="Justify.SpaceBetween">
            <MudStack Row AlignItems="AlignItems.Start">
                <MudIcon Icon="@Icons.Material.Filled.Domain" Size="Size.Large" />
                <MudStack Spacing="0">
                    <MudText Typo="Typo.subtitle2" Class="mb-2">@Title</MudText>
              
                </MudStack>
            </MudStack>
            <MudStack Spacing="0" AlignItems="AlignItems.End">
                <MudToolBar Dense WrapContent="true" Class="py-1 px-0">
                    <MudButton 
                               Disabled="@_loading"
                               OnClick="@(() => OnRefresh())"
                               StartIcon="@Icons.Material.Outlined.Refresh">
                        @ConstantString.Refresh
                    </MudButton>
                    @if (_canCreate)
                    {
                        <MudButton 
                                   StartIcon="@Icons.Material.Outlined.Add"
                                   OnClick="OnCreate">
                            @ConstantString.New
                        </MudButton>
                    }
                    @if (_canDelete)
                    {
                        <MudButton 
                                   StartIcon="@Icons.Material.Outlined.Delete"
                                   Disabled="@(!(_selectedItems.Count > 0))"
                                   OnClick="OnDeleteChecked">
                            @ConstantString.Delete
                        </MudButton>
                    }
                </MudToolBar>
                <MudStack Row Spacing="1">
                    @if (_canSearch)
                    {
                        <MudTextField T="string" ValueChanged="@(s => OnSearch(s))" Value="@Query.Keyword" Placeholder="@ConstantString.Search" Adornment="Adornment.End"
                                      AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Small">
                        </MudTextField>
                    }
                </MudStack>
            </MudStack>
        </MudStack>
    </ToolBarContent>
    <Columns>
        <SelectColumn ShowInFooter="false"> </SelectColumn>
        <TemplateColumn HeaderStyle="width:60px" Title="@ConstantString.Actions" Sortable="false">
            <CellTemplate>
                @if (_canEdit || _canDelete)
                {
                    <MudMenu Icon="@Icons.Material.Filled.Edit" Variant="Variant.Filled" Size="Size.Small"
                             Dense="true"
                             EndIcon="@Icons.Material.Filled.KeyboardArrowDown" IconColor="Color.Info" AnchorOrigin="Origin.CenterLeft">
                        @if (_canEdit)
                        {
                            <MudMenuItem OnClick="@(() => OnEdit(context.Item))">@ConstantString.Edit</MudMenuItem>
                        }
                        @if (_canDelete)
                        {
                            <MudMenuItem OnClick="@(() => OnDelete(context.Item))">@ConstantString.Delete</MudMenuItem>
                        }
                    </MudMenu>
                }
                else
                {
                    <MudButton Variant="Variant.Filled"
                               StartIcon="@Icons.Material.Filled.DoNotTouch" IconColor="Color.Secondary" Size="Size.Small"
                               Color="Color.Surface">
                        @ConstantString.NoAllowed
                    </MudButton>
                }
            </CellTemplate>
        </TemplateColumn>
        <PropertyColumn  Property="x => x.Name" Title="@L[_currentDto.GetMemberDescription(x => x.Name)]">
            <FooterTemplate>
                @L["Selected"]: @_selectedItems.Count
            </FooterTemplate>
        </PropertyColumn>
        <PropertyColumn Property="x => x.Description" Title="@L[_currentDto.GetMemberDescription(x => x.Description)]">
            <CellTemplate>
                <div class="d-flex flex-column">
                    <MudText Typo="Typo.body2" Class="mud-text-secondary">@context.Item.Description</MudText>
                </div>
            </CellTemplate>
        </PropertyColumn>

    </Columns>

    <PagerContent>
        <MudDataGridPager PageSizeOptions="@(new[] { 10, 15, 30, 50, 100, 500, 1000 })" />
    </PagerContent>
</MudDataGrid>

@code
{
    [CascadingParameter] private Task<AuthenticationState> AuthState { get; set; } = default!;
    public string? Title { get; private set; }
    private int _defaultPageSize = 15;
    private HashSet<TenantDto> _selectedItems = new();
    private MudDataGrid<TenantDto> _table = default!;
    private TenantDto _currentDto = new();
    private string _searchString = string.Empty;
    private bool _loading;
    private bool _canSearch;
    private bool _canCreate;
    private bool _canEdit;
    private bool _canDelete;

    private TenantsWithPaginationQuery Query { get; } = new();

    protected override async Task OnInitializedAsync()
    {
        Title = L[_currentDto.GetClassDescription()];
        var state = await AuthState;
        _canCreate = (await AuthService.AuthorizeAsync(state.User, Permissions.Tenants.Create)).Succeeded;
        _canSearch = (await AuthService.AuthorizeAsync(state.User, Permissions.Tenants.Search)).Succeeded;
        _canEdit = (await AuthService.AuthorizeAsync(state.User, Permissions.Tenants.Edit)).Succeeded;
        _canDelete = (await AuthService.AuthorizeAsync(state.User, Permissions.Tenants.Delete)).Succeeded;
    }

    private async Task<GridData<TenantDto>> ServerReload(GridState<TenantDto> state)
    {
        try
        {
            _loading = true;
            Query.Keyword = _searchString;
            Query.OrderBy = state.SortDefinitions.FirstOrDefault()?.SortBy ?? "Id";
            Query.SortDirection = state.SortDefinitions.FirstOrDefault()?.Descending ?? true ? SortDirection.Descending.ToString() : SortDirection.Ascending.ToString();
            Query.PageNumber = state.Page + 1;
            Query.PageSize = state.PageSize;
            var result = await Mediator.Send(Query);
            return new GridData<TenantDto> { TotalItems = result.TotalItems, Items = result.Items };
        }
        finally
        {
            _loading = false;
        }
    }

    private async Task OnSearch(string text)
    {
        _selectedItems = new HashSet<TenantDto>();
        _searchString = text;
        await _table.ReloadServerData();
    }

    private async Task OnRefresh()
    {
        TenantCacheKey.Refresh();
        _selectedItems = new HashSet<TenantDto>();
        _searchString = string.Empty;
        await _table.ReloadServerData();
    }
    private async Task ShowEditDialog(AddEditTenantCommand command, string title)
    {
        var parameters = new DialogParameters<TenantFormDialog>
        {
            { x => x.Model, command }
        };
        var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Small, FullWidth = true };
        var dialog =await DialogService.ShowAsync<TenantFormDialog>(title, parameters, options);
        var state = await dialog.Result;
        if (state is not null &&  !state.Canceled)
        {
            await _table.ReloadServerData();
        }
    }
    private async Task OnCreate()
    {
        await ShowEditDialog(new AddEditTenantCommand(), L["Create a new Tenant"]);
    }

    private async Task OnEdit(TenantDto dto)
    {
        var command = TenantMapper.ToEditCommand(dto);
        await ShowEditDialog(command, L["Edit the Tenant"]);
    }

    private async Task OnDelete(TenantDto dto)
    {
        var command = new DeleteTenantCommand(new[] { dto.Id });
        var contentText = string.Format(ConstantString.DeleteConfirmationWithId, dto.Id);
        await DialogServiceHelper.ShowDeleteConfirmationDialogAsync(command, L["Delete the Tenant"], contentText, async () =>
        {
            await InvokeAsync(async () =>
        {
            await _table.ReloadServerData();
            _selectedItems.Clear();
        });
        });
    }

    private async Task OnDeleteChecked()
    {
        var command = new DeleteTenantCommand(_selectedItems.Select(x => x.Id).ToArray());
        var contentText = string.Format(ConstantString.DeleteConfirmation, _selectedItems.Count);
        await DialogServiceHelper.ShowDeleteConfirmationDialogAsync(command, L["Delete Selected Tenants"], contentText, async () =>
        {
            await InvokeAsync(async () =>
            {
                await _table.ReloadServerData();
                _selectedItems.Clear();
            });
        });
    }
}